We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

ERROR SQLSTATE[HY093] when Im try to $query->save() from the Controller

I'm trying to do a simple update on the database but I got an error, I think its is a PDO issue, but I couldn't find the solution, I don't know what more can I do, I use Phalcon 2.0.7

ERROR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

Example Url: https://domain.com/controller/hide/8

On my Controller:

public function hideAction($id) {
        $query = Table::findFirst($id);
        $query->active = 0;
        $query->save();
}

but also I try this:

public function hideAction($id) {
        $query = Table::findFirst($id);
        $query->save([
            'active' => 0
        ]);
}

On my Model:

use Phalcon\Mvc\Model;

class Table extends Model {

    public $id;

    public $active;

    public $title;

    public function initialize() {

    }

    public function getSource() {
    return 'table_sample';
    }

That my dispatcher service for the database connection:

$di->set('db', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Mysql(
            array(
            "host" => $config->db->host,
            "username" => $config->db->username,
            "password" => $config->db->password,
            "dbname" => $config->db->dbname
            )
        );
});

As you can see is a simple update, probably Im missing something



85.5k

hmmm can we see the print_r of $query = Table::findFirst($id);

usually this error happens when binded params are messed up, but since phalcon take cares of it I have my doubts it's you that did something wrong.

can you check if you add those configs to your connection if it changes anything, perhaps an utf8 problem ?


                $class = new \Phalcon\Db\Adapter\Pdo\Mysql([
                  'host' => $config->database->host,
                  'username' => $config->database->username,
                  'password' => $config->database->password,
                  'dbname' => $config->database->dbname,
                  'port' => '3306',
                  'charset' => 'utf8',
                  "options"  => [
                      \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                      \PDO::ATTR_PERSISTENT => true,
                      \PDO::ATTR_EMULATE_PREPARES => false,
                      \PDO::ATTR_DEFAULT_FETCH_MODE  =>  \PDO::FETCH_ASSOC,
                      \PDO::ATTR_STRINGIFY_FETCHES => false
                  ]
              ]);

I found the issue, I have an afterFetch() function on my Model handle some string formatting, the model try to handle checking if exists a beforeSave (I didn't used), so I create a beforeSave and that fix the issue. Thank you anyway!!